<?php
App::uses('AppModel', 'Model');
/**
 * Subscriber Model
 *
 */
class Subscriber extends AppModel {

/**
 * Primary key field
 *
 * @var string
 */
public $primaryKey = 'sub_id';

    public $validate = array(
		
		'sub_name' => array( 
			'sub_must_have_a_name' => array( 
			'rule' => array('notempty'), 
			'message' => 'Please enter full name'), 

			'custom' => array (
			// a bit more lenient for names as follows
			// Donalg McDonald <- capital in the middle of a word
			//Donalg Fooble-Whiffery <- hyphenated
			//Donalg Déénsbnérry-McDonald Sr. 3rd <- number in the name
            'rule' => array('custom', '/([\w.-]+ )+[\w+.-]/'),
            'message' => 'Please enter an appropriate name'),

			
			),
	
    	'sub_email' => array(
    		
    		'sub_email_must_be_entered' => array(
			'rule' => array('notempty'),
			'required' => true,
			'message' => 'Please enter an email address.'),

			'identicalFieldValues' => array( 
        	'rule' => array('identicalFieldValues', 'confirm_email' ), 
        	'message' => 'Email address does not match confirm email address, please re-enter them'), 

			'Unique_Email' => array (
			'rule' => 'isUnique',
			'required' => true,
			'message' => 'This email address already exists in our database, please try again.'),

        	'valid_email_format' => array (
        	'rule' => array('email', false),
			'message' => 'You have entered an invaild email format, please try again'),

    		),

    );

   function identicalFieldValues( $field=array(), $compare_field=null )  
    { 
        foreach( $field as $key => $value ){ 
            $v1 = $value; 
            $v2 = $this->data[$this->name][ $compare_field ];                  
            if($v1 !== $v2) { 
                return FALSE; 
            } else { 
                continue; 
            } 
        } 
        return TRUE; 
    } 

 
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'sub_email';

}